﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkSummary.Business;
using WorkSummary.Bussiness.Dal;
using WorkSummary.Bussiness.Model;
using WorkSummary.Common;
using WorkSummary.Common.Data;
using WorkSummary.Common.log;
using WorkSummary.Common.Provider;

namespace WorkSummary.Bussiness.Bll
{
    public class BReport
    {
        public static BReport Instance
        {
            get { return SingletonProvider<BReport>.Instance; }
        }

        public TopResult AddReport(MReport r)
        {
            try
            {
                string sql = "select * from t_report where ReportDate = '" + r.ReportDate + "' and IsDelete = 0 and UserID = '" + SysVisitor.Instance.UserID + "'";
                var dt = DbUtils.GetTable(sql);
                if (dt.Rows.Count != 0)
                {
                    return new TopResult("已经存在当前日期的日报");
                }
                r.ReportID = Guid.NewGuid().ToString();
                r.CreateTime = DateTime.Now;
                r.UserID = SysVisitor.Instance.UserID;
                r.UserName = SysVisitor.Instance.UserName;
                r.IsDelete = 0;
                DReport.Instance.Insert(r);
                log.info(SysVisitor.Instance.AccountNum, "新增日报", "新增" + r.ReportDate.ToString("d") + "日报:" + r.ReportID);
                return new TopResult();
            }
            catch (Exception ex)
            {
                log.error(SysVisitor.Instance.AccountNum, "新增日报", "新增日报出错", ex);
                return new TopResult(-1, ex.Message);
            }
        }

        public TopResult ModifyReport(MReport r)
        {
            try
            {
                var oldReport = DReport.Instance.Get("ReportID", r.ReportID);
                if (oldReport.ReportDate != r.ReportDate)
                {
                    string sql = "select * from t_report where ReportDate = '" + r.ReportDate + "' and IsDelete = 0 and UserID = '" + oldReport.UserID + "'";
                    var dt = DbUtils.GetTable(sql);
                    if (dt.Rows.Count != 0)
                    {
                        return new TopResult("已经存在当前日期的日报");
                    }
                }
                r.CreateTime = oldReport.CreateTime;
                r.UserID = oldReport.UserID;
                r.UserName = oldReport.UserName;
                DReport.Instance.Update("ReportID", r);
                log.info(SysVisitor.Instance.AccountNum, "修改日报", "修改" + r.ReportDate.ToString("d") + "日报:" + r.ReportID + r.ReportInfo);
                return new TopResult();
            }
            catch (Exception ex)
            {
                log.error(SysVisitor.Instance.AccountNum, "修改日报", "修改日报出错", ex);
                return new TopResult(-1, ex.Message);
            }
        }

        public TopResult DelReport(string ids)
        {
            try
            {
                string sql = string.Format("update t_report set IsDelete='1' where ReportId in ({0})", ids);
                DbUtils.ExecuteNonQuery(sql);
                log.info(SysVisitor.Instance.AccountNum, "删除日报", "删除日报" + ids);
                return new TopResult();
            }
            catch (Exception ex)
            {
                log.error(SysVisitor.Instance.AccountNum, "删除日报", "删除日报出错", ex);
                return new TopResult(-1, ex.Message);
            }
        }

        public List<MReport> GetAllReportByUser(string sdate, string edate)
        {
            string where = "IsDelete=0";
            if (!SysVisitor.Instance.UserName.Contains("管理员"))
            {
                where += " and UserID = '" + SysVisitor.Instance.UserID + "'";
            }
            if (!string.IsNullOrEmpty(sdate))
            {
                where += " and ReportDate >'" + sdate + " 00:00:00'";
            }
            if (!string.IsNullOrEmpty(edate))
            {
                where += " and ReportDate <'" + edate + " 23:59:59'";
            }
            var list = DReport.Instance.GetAll(where).ToList();
            return list;
        }
        public List<MReport> GetAllReport(string sdate, string edate)
        {
            string where = "IsDelete=0";
            if (!string.IsNullOrEmpty(sdate))
            {
                where += " and ReportDate >'" + sdate + " 00:00:00'";
            }
            if (!string.IsNullOrEmpty(edate))
            {
                where += " and ReportDate <'" + edate + " 23:59:59'";
            }
            var list = DReport.Instance.GetAll(where).ToList();
            return list;
        }
    }
}
